<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/6
 * Time: 17:26
 */
namespace App\Http\Controllers;

use App\Flight;
use Illuminate\Support\Facades\App;

class FlightController extends Controller
{

    public function index()
    {
        //获取所有数据
        $flights = Flight::all();
        $flights = Flight::where('id',1)->get();

        //分块结果，节省内存
//        Flight::chunk(100, function ($flights) {
//            foreach ($flights as $flight)
//            {
//                //
//            }
//        });
        //使用游标，只执行一个查询，处理大量数据时，可以大幅度减少内存的使用量
        foreach(Flight::where('id','>=',1)->cursor() as $flight)
        {
            //dd($flight);
        }
        //获取某一条数据
        $flight = Flight::find(2);
        $flight = Flight::where('id',2)->first();
        dd($flight);
        //dd($flights);

        //聚合函数
        $count = Flight::where('id','>=',1)->count();
        $max = Flight::where('id','>=', 1)->max('id');
    }

    public function add()
    {
        $flight = new Flight();
        $flight->name = 'boyin123';

        $row = $flight->save();
        dd($row);
    }

    public function update()
    {
        $flight = Flight::find(1);

        $flight->name = 'new name';
        $flight->save();
    }

    public function delete() {
        Flight::destroy(1);
    }
}